package com.caicai.util;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class BaseUtil {
	private volatile static SqlSession sqlSession;
	/**
	 * 单例模式获取会话
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static SqlSession getSqlSession(String fileName) throws Exception{
		synchronized (BaseUtil.class) {
			if(sqlSession==null){
				synchronized (SqlSession.class) {
					//得到配置文件字节输入流
					InputStream is = Resources.getResourceAsStream(fileName);
					 //得到会话工厂
					SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
					SqlSessionFactory sqlSessionFactory = factoryBuilder.build(is);
					sqlSession = sqlSessionFactory.openSession();
				}
			}
		}
		return sqlSession;
	}
}
